<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $d=new UserController();
        $d->login_check(1);
        $loginInfo=D('login_info');
        $loginSum=$loginInfo->count();
        $date=date('Y-m-d');
        $map['loginTime'] = array('LIKE','%'.$date.'%');
        $loginTodaySum=$loginInfo->where($map)->count();
        $info=$loginInfo->order('loginTime desc')->limit(4)->select();
//        dump($info);die;

        $sendInfo=D('send_info');
        $deviceSum=$sendInfo->count();

        $userInfo=D('user_info');
        $userSum=$userInfo->count();

        $this->assign('loginTodaySum',$loginTodaySum);
        $this->assign('loginSum',$loginSum);
        $this->assign('userSum',$userSum);
        $this->assign('deviceSum',$deviceSum);
        $this->assign('info',$info);
        $this->display();
    }
    /**
     * 分类搜索
     * 按地区
     * 按类型
     * 按楼宇
     */
    public function search(){
        $info = M(send_info);
        $infoResult = $info->distinct(true)->field('deviceType')->select();
        $infoResult1 = $info->distinct(true)->field('regionNum')->select();
        // dump($infoResult1[0]);die;
        $this->assign("info",$infoResult);
        $this->assign("info1",$infoResult1);
        $this->display();
    }
    public function userMap(){
        $this->display();
    }
    public function getMap(){
        $buildingInfo = M(storey_info);
        $deviceInfo = M(send_info);
        $buildingName = $buildingInfo->distinct(true)->field('buildingNum')->select();
        foreach ($buildingName as $key => $value) {
            $res[$key] = $buildingInfo->where(array('buildingNum' => $value['buildingnum']))->find();
            $res[$key]['deviceCount'] = $deviceInfo->where(array('buildingNum' => $value['buildingnum']))->count();
            // dump($res[$key]);
        }
        // $building_number = $buildingInfo->count();
        // dump(json_encode($res));
        $this->ajaxReturn(json_encode($res),'json');
        // $this->assign("number",$building_number);
        // $this->assign("info",$res);
        // $this->display();
    }
    /**
     * [classifySearch description]
     * @return [type] [description]
     * regionNum/deviceType/chipId
     */
    public function classifySearch(){
        // dump($_POST);
        // die;
        $search_data = I("post.");
        $npm['regionNum'] = $search_data['regionNum'];
        $npm0['deviceType'] = $search_data['deviceType'];
        $npm1['chipId'] = $search_data['chipId'];

        // echo "ds".$npm['regionNum'];
        // dump($npm1);
        $sendInfo = M("send_info");
        $infoResult = $sendInfo->distinct(true)->field('regionNum')->select();
        // dump($infoResult);
        // die;
        // foreach ($infoResult as $n => $regionNum) {
            if($search_data['chipId'] == ''){
                if($search_data['regionNum'] != ''){
                    if($search_data['deviceType'] != ''){
                        $res = $sendInfo->where($npm)->where($npm0)->select();
                        // echo "111";
                        // dump($res);
                        // die;
                    }else{
                        $res = $sendInfo->where($npm)->select();
                    //     echo "222";
                        // dump($res);
                    //     die;
                    }
                }else{

                    if($search_data['deviceType'] != ''){
                        $res = $sendInfo->where($npm0)->select();
                        // echo "333";
                        // dump($res);
                        // die;
                    }else{
                        foreach ($infoResult as $n => $value) {
                            $npm3['regionNum'] = $value['regionnum'];
                            // echo "dsds".$value['regionnum'];
                            // dump($value);/
                            // $res[$n] = $value['regionnum'];
                            $res[$n]['regionNum'] = $value['regionnum'];
                            $res[$n]['val'] = $sendInfo->where($npm3)->select();
                            // dump($res);
                            // dump($res[$n]);
                            // die;
                        }
                        // die;
                        // echo "444";
                        // dump($res[$n]['regionnum']);
                        // die;
                    }
                }
            }else{
                $res = $sendInfo->where($npm1)->find();
                // echo "555";
                // dump($res);
                // die;

            }
            // die;
        // }
        // dump($res);die;
        // $this->ajaxReturn($res);
        $this->ajaxReturn(json_encode($res),'json');
        // $this->assign("res",$res);
        // $this->display();
        
    }
}